package com.abc.recycling.controller.admin;

import com.abc.recycling.model.NotificationTemplate;
import com.abc.recycling.service.NotificationTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/notification-templates")
@RequiredArgsConstructor
public class NotificationTemplateController {

    private final NotificationTemplateService templateService;

    @GetMapping
    public String listTemplates(Model model) {
        model.addAttribute("templates", templateService.getAllTemplates());
        return "admin/notification/templates";
    }

    @GetMapping("/api/list")
    @ResponseBody
    public Page<NotificationTemplate> getTemplates(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return templateService.getTemplatesByPage(PageRequest.of(page, size));
    }

    @GetMapping("/api/{id}")
    @ResponseBody
    public ResponseEntity<NotificationTemplate> getTemplate(@PathVariable Long id) {
        return ResponseEntity.ok(templateService.getTemplateById(id));
    }

    @PostMapping("/api")
    @ResponseBody
    public ResponseEntity<NotificationTemplate> createTemplate(@RequestBody NotificationTemplate template) {
        return ResponseEntity.ok(templateService.createTemplate(template));
    }

    @PutMapping("/api/{id}")
    @ResponseBody
    public ResponseEntity<NotificationTemplate> updateTemplate(
            @PathVariable Long id,
            @RequestBody NotificationTemplate template) {
        template.setId(id);
        return ResponseEntity.ok(templateService.updateTemplate(template));
    }

    @DeleteMapping("/api/{id}")
    @ResponseBody
    public ResponseEntity<Void> deleteTemplate(@PathVariable Long id) {
        templateService.deleteTemplate(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/api/types")
    @ResponseBody
    public ResponseEntity<List<NotificationTemplate>> getTemplatesByType(@RequestParam String type) {
        return ResponseEntity.ok(templateService.getTemplatesByType(type));
    }

    @PostMapping("/api/{code}/process")
    @ResponseBody
    public ResponseEntity<String> processTemplate(
            @PathVariable String code,
            @RequestBody Map<String, Object> data) {
        return ResponseEntity.ok(templateService.processTemplate(code, data));
    }
} 